# 闭包!
# 闭包的3大特征:
# 1. 函数一定要产生嵌套
# 2. 内部函数使用了外部函数的变量
# 3. 内部函数被返回出去(内部函数在外部函数执行完了再执行)
# def outer():
#     a = 1
#     def inner():
#         print(a)
#     return inner
#
# inner = outer()
# inner()


# 装饰器
# def test(a=1,b=2,c=3):
#     print("this is test",a,b,c)
#     return a+b+c
#
# def log(fn):
#     def wrapper(a, b, c):
#         print(fn.__name__ + "函数开始执行了...")
#         res = fn(a, b, c)
#         print(fn.__name__ + "函数执行结束了...")
#         return res
#     return wrapper
#
# test = log(test)
#
# res = test(2,3,4)
# print(res)

# 例题
# 2.阅读代码题 (打印什么? 为什么?)
def create_adders():
    adders = []
    for i in range(3):
        def adder(x):
            return i + x
        adders.append(adder)
    return adders


# create_adders():[adder,adder,adder]
for adder in create_adders():
    print(adder(10))

# 阅读题
def outer():
    x = 10
    lst = []
    for i in range(3):
        def inner():
            nonlocal x
            x += 1
            return x * i
        lst.append(inner)
    return lst

funcs = outer()
results = [func() for func in funcs]
print(results)